import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import { Icon } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import DetailInfo from "./Components/DetailInfo";
import CloseWarn from "./Components/CloseWarn";
import styles from '../Pbulic.less';


@connect(({ global }) => ({
  dictionaries:global.dictionaries,
}))
export default class EquipmentDetail extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      modalWarnCloseVisible: false,
      modalWarnCloseinfo:null,
    };
  }


  componentDidMount() {

  }

  componentWillUnmount(){
    const { dispatch } = this.props;
    dispatch({
      type: `warn/appendClearState`,
    })
  }

  // 关闭告警信息
  handleCloseWarn = (row) => {
    this.setState({
      modalWarnCloseinfo: row,
    },()=>{
      this.handleCloseWarnModalVisible(true);
    });
  };

  // 显示关闭告警信息model
  handleCloseWarnModalVisible = flag => {
    this.setState({
      modalWarnCloseVisible: !!flag,
    });
  };

  // 填写关闭告警原因后回调
  handleCloseWarnModelCallBack=(value,reinfo)=>{
    this.handleCloseWarnModalVisible(false);
    const { dispatch } = this.props;
    const { modalWarnCloseinfo } = this.state;

    if(value && modalWarnCloseinfo){
      dispatch({
        type: 'warn/action_warn_close',
        payload: {
          warnId:modalWarnCloseinfo.warnId,
          ...value,
        },
      }).then(()=>{
        if(modalWarnCloseinfo.from=="info"){
          dispatch({
            type: "warn/fetch_warn_info",
            payload: {
              warnId: modalWarnCloseinfo.warnId
            }
          });
        }
        this.setState({
          modalWarnCloseinfo: null,
          isReloadList:true,
        });

        // this.getDataList(formValues,pageSize,currentPage);
      });
    }

  }


  goBack = () => {
    const { dispatch, match: {path} ,location } = this.props;
    console.log(this.props);
    if(path=="/alarm/currentalarm/:id"){
      router.push({ pathname:'/alarm/currentalarm/list',  query: { ...location.query} });
    }
    if(path=="/alarm/historyalarm/:id"){
      router.push({ pathname:'/alarm/historyalarm/list',  query: { ...location.query} });
    }
  }

  render() {
    const { match, dictionaries } = this.props;
    const { modalWarnCloseVisible, modalWarnCloseinfo } = this.state;

    const detailInfoId = match.params.id;


    return (
      <Fragment>
        <Icon type="close-circle-o"  className={styles.close_icon} onClick={this.goBack}/>
        <PageHeaderLayout
          hiddenBreadcurmb={true}
          title={<span><Icon type="exclamation-circle" style={{marginRight:10,color:'#FFD113'}}/>告警详情</span>}
        >
          <DetailInfo detailInfoId={detailInfoId}  closeWarnCallBack={this.handleCloseWarn}/>

          <CloseWarn
            visible={modalWarnCloseVisible}
            onCancel={e => this.handleCloseWarnModalVisible(e)}
            callback={this.handleCloseWarnModelCallBack}
            warninfo={modalWarnCloseinfo}
            dictionaries={dictionaries}
          />
        </PageHeaderLayout>
      </Fragment>
    );
  }
}
